<template>
	<view class="content update">
		<view class="input-group">
			<view class="input-row border">
				<text class="title"><span class="i-color">* </span>旧密码:</text>
				<input type="password" class="input-text" displayable v-model="pwd" placeholder="请输入旧密码"></input>
			</view>
			<view class="input-row border">
				<text class="title"><span class="i-color">* </span>新密码:</text>
				<input type="password" class="input-text" displayable v-model="password" placeholder="请输入密码"></input>
			</view>
			<view class="input-row">
				<text class="title"><span class="i-color">* </span>新密码:</text>
				<input type="password" class="input-text" displayable v-model='passwords' placeholder="请重新输入密码"></input>

			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="register">修改</button>
		</view>
	</view>
</template>

<style lang="scss">
	.update {
		.input-row {
			.input-text {
				flex: 1;
				min-height: 40upx;
				height: 40upx;
				line-height: 40upx;
				font-size: 28upx;
				padding: 20upx 0;
			}
		}
	}

	uni-input,
	uni-input input {
		min-height: 80upx;
	}
</style>

<script>
	import api from '../../api.js';

	export default {
		components: {
		},
		data() {
			return {
				pwd: '',
				password: '',
				networkType:'',
				userinfo: uni.getStorageSync('user')
			}
		},
		onLoad: function(options) {
			uni.getNetworkType({
					success: (res) => {
					console.log(res)
				  this.networkType = res.subtype || res.networkType
				},
				fail: () => {
				    uni.showModal({
				    	content:'获取失败！',
				        showCancel:false
				    })
				}
			});
		},
		methods: {
			register() {
				if (this.pwd.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请输入旧密码'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				if (this.password != this.passwords) {
					uni.showToast({
						icon: 'none',
						title: '密码不一致'
					});
					return;
				}
				if (this.networkType.length != 0) {//没有及时获取到正确网络状态重新获取一般
						uni.getNetworkType({
							success: (res) => {
							console.log(res)
						  this.networkType = res.subtype || res.networkType
						},
						fail: () => {
						    uni.showModal({
						    	content:'获取失败！',
						        showCancel:false
						    })
						}
					});
				}
				
				
				
				//加载
				uni.showLoading({
					title: 'loading'
				});
				if(this.networkType=='none'){
					uni.hideLoading();//加载完毕
					uni.showModal({
						content: '网络异常 无法访问服务器',
						confirmText: "取消",
						cancelText: "确定"
					})
					  return; 
				}
				const data = {
					pwd: this.pwd,
					password: this.password,
					id: this.userinfo.user.id
				}
				api.post({
					url: 'api/updatePwd.do',
					data: data,
					success(res) {
						uni.hideLoading();//加载完毕
						if (res.isSuccess) {
							// uni.setStorageSync('user', res.data)
							uni.showToast({
								icon: 'none',
								title: res.data,
							});
							uni.reLaunch({
							   url: '../login/login',
						});
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message,
							});
						}
					}
				})
				// service.addUser(data);
				// 	                uni.showToast({
				// 	                    title: '修改成功'
				// 	                });
				// 	                uni.navigateBack({
				// 	                    delta: 1
				// 	                });
			}
		}
	}
</script>
